package me.dqn.callback;

import me.dqn.aspect.ProxyAspect;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author dqn
 * 2018/5/12--16:46
 * call back for before call
 */
public class BeforeCallBack implements MethodInterceptor {
    Class<? extends ProxyAspect> aspect;

    public BeforeCallBack(Class<? extends ProxyAspect> aspect) {
        this.aspect = aspect;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        ProxyAspect instance = aspect.newInstance();
        if (method.getParameterCount() > 0) {
            instance.doBefore(objects);
        } else {
            instance.doBefore();
        }
        return methodProxy.invokeSuper(o, objects);
    }
}
